#include "BlockQuene.hpp"
#include <pthread.h>
#include <unistd.h>
#include "Task.hpp"
#include <ctime>

void *Consumer(void *args)
{
    BlockQuene<task_t> *bq = static_cast<BlockQuene<task_t>*>(args);
    while(true)
    {
        //1.获取数据
        task_t t;
        bq->Pop(&t);
        // t.Excute();
        t();
        //2.处理数据
        //std::cout << "Consumer -> " << t.result() << std::endl; 
    }
}

void *Productor(void *args)
{
    srand(time(nullptr));
    BlockQuene<task_t> *bq = static_cast<BlockQuene<task_t> *>(args);
    while(true)
    {
        sleep(2);
        //1.构建数据
        // int x = rand() % 10 + 1;
        // usleep(x*1000);
        // int y = rand() % 10 + 1;
        // task_t t(x,y);
        //2.生产数据
        bq->Equeue(Download);
        std::cout << "Productor -> Download " << std::endl; 
    }
}

int main()
{
    BlockQuene<task_t>* bq = new BlockQuene<task_t>();
    pthread_t c, p;
    pthread_create(&c, nullptr, Consumer, (void *)bq);
    pthread_create(&p, nullptr, Productor, (void *)bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
}